<template>
  <div class="dashboard-container">
    <div class="dashboard-text">
      <el-card>
        <!-- 搜索栏 -->
        <el-row type="flex" justify="space-between">
          <el-col :span="8">
            <el-button type="primary" @click="isShowDialog = true">添加经纪</el-button>
          </el-col>
          <el-col :span="8">
            <el-input v-model="name_contains" placeholder="请输入关键字" class="input-with-select">
              <el-button slot="append" icon="el-icon-search" @click="search" />
            </el-input>
          </el-col>
        </el-row>
        <hr>
        <!-- 表格 -->
        <el-table border :data="tableData">
          <el-table-column label="图片" sortable="" align="center">
            <template #default="{row}">
              <img :src="row.image" alt="" class="staffPhoto" style="border-radius: 50%; width: 100px; height: 100px; padding: 10px">
            </template>
          </el-table-column>
          <el-table-column label="名称" sortable="" prop="name" />
          <el-table-column label="邮箱" align="center" prop="email" />
          <el-table-column
            label="电话"
            sortable=""
            prop="phone"
          />
          <el-table-column
            label="地址"
            sortable=""
            prop="address"
          />
          <el-table-column
            label="介绍"
            sortable=""
            prop="desc"
          />
          <el-table-column label="操作" sortable="" fixed="right" width="280" align="center">
            <template #default="{row}">
              <el-button type="primary" @click="upDate(row)">编辑</el-button>
              <el-button type="danger" @click="btnDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <hr>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <el-pagination
            layout="prev,pager,next"
            :total="total"
            :page-size="limit"
            @current-change="currentChange"
          />
        </el-row>
      </el-card>
      <!-- 弹窗 -->
      <el-dialog :title="formDate.id?'编辑经纪':'添加经纪'" :visible="isShowDialog" @close="btnCancel">
        <el-form ref="form" :model="formDate" label-width="80px" :rules="rules">
          <el-form-item label="名称" prop="name">
            <el-input v-model="formDate.name" />
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="formDate.email" />
          </el-form-item>
          <el-form-item label="电话" prop="phone">
            <el-input v-model="formDate.phone" />
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input v-model="formDate.address" />
          </el-form-item>
          <el-form-item label="描述" prop="desc">
            <el-input v-model="formDate.desc" />
          </el-form-item>
          <el-form-item label="封面">
            <ImageUpload ref="formDataPhoto" />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-row>
            <el-button type="primary" @click="btnOk">确定</el-button>
            <el-button @click="btnCancel">取消</el-button>
          </el-row>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getAgentsList, getAgentsLength, delAgents, addAgents, upDataAgents } from '@/api/agents'

export default {

  data() {
    return {
      total: 0,
      isShowDialog: false,
      limit: 10,
      start: 0,
      name_contains: '',
      tableData: [],
      formDate: {
        name: '',
        email: '',
        phone: '',
        address: '',
        desc: '',
        // image: 'https://img1.baidu.com/it/u=2923987797,1650495528&fm=26&fmt=auto'
        image: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '手机号不能为空', trigger: 'blur' }, {
            pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
          }
        ],
        address: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '请输入具体描述', trigger: 'blur' }

        ]
      }
    }
  },
  created() {
    this.getAgentsList()
    this.getAgentsLength()
    // this.getUser()
  },
  methods: {
    async getAgentsList() {
      const { data } = await getAgentsList({ _limit: this.limit, _start: this.start, name_contains: this.name_contains })
      this.tableData = data
      console.log(this.tableData, 'des ')
    },
    async getAgentsLength() {
      const { data } = await getAgentsLength()
      // console.log(data, '数量')
      this.total = data
    },
    currentChange(newPage) {
      // console.log(newPage, 'newPage')
      this.start = newPage
      this.getAgentsList()
    },
    async btnDel(id) {
      try {
        await this.$confirm('确定要删除该经纪吗?')
        // console.log(id.toString(), 'id')
        await delAgents(id.toString())
        this.$message.success('删除成功')
        await this.getAgentsList()
      } catch (error) {
        console.log(error)
      }
    },
    upDate(data) {
      console.log(data, '回显数据')
      this.isShowDialog = true
      this.formDate = data
      this.$nextTick(() => {
        this.$refs.formDataPhoto.fileList = [{ url: this.formDate.image }]
      })
      console.log(this.formDate, 'this.$refs.form.formDate')
    },
    async btnOk() {
      await this.$refs.form.validate()
      const fileList = this.$refs.formDataPhoto.fileList
      console.log(fileList, 'fileList')
      if (fileList[0] && fileList[0].status !== 'success') {
        this.$message.warning('请耐心等待图片上传完毕')
        return
      }
      if (this.formDate.id) {
        await this.$confirm('确定要修改该经纪吗?')
        await upDataAgents({ ...this.formDate, id: this.formDate.id.toString(), image: fileList ? fileList[0].url : '' })
        this.$message.success('修改成功')
        this.isShowDialog = false
      } else {
        await this.$confirm('确定要添加该经纪吗?')
        await addAgents({ ...this.formDate, image: fileList ? fileList[0].url : '' })
        await this.$message.success('添加成功')
        this.isShowDialog = false
      }
      await this.getAgentsList()
    },
    btnCancel() {
      this.isShowDialog = false
      this.$refs.form.resetFields()
      this.formDate = {
        name: '',
        email: '',
        phone: '',
        address: '',
        desc: '',
        image: ''
      }
      this.$refs.formDataPhoto.fileList = []
    },
    async getUser() {
      this.tableData = await getAgentsList()
      if (this.tableData.staffPhoto) {
        this.$refs.formDataPhoto.fileList = [
          { url: this.tableData.image }
        ]
      }
      console.log(this.tableData.data, 'this.tableData')
    },
    search() {
      console.log(this.name_contains)
      this.getAgentsList()
      this.name_contains = ''
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
